import { Collection } from 'collection-model.js';
var collection = new Collection();
Page({
  data: {
    loadingHidden:false,
    searchLoading: false,
    searchLoadingComplete: false,
    page:1
  },
  onLoad: function (options) {
    this._loadData();
    
  },
  _loadData:function(){
    collection.getCollectionLists(this.data.page,(res)=>{
      this.setData({
        productData:res.items,
        loadingHidden:true,
        pageCount:res._meta.pageCount,
      })
      if (res._meta.pageCount==1){
        this.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
      if (res._meta.pageCount == 0) {
        this.setData({
          searchLoading: false,
          searchLoadingComplete: false,
        })
      }
    })
  },

  //触底滚动加载
  onReachBottom: function () {
    var page = parseInt(this.data.page) + 1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })
      collection.getCollectionLists(page,(res) => {
        this.scrollLoadData(res, page);
      })
    }
  },
  //拼接数据
  scrollLoadData: function (res, page) {
    var data = res.items;
    var productData = this.data.productData;
    productData = productData.concat(data)
    this.setData({
      productData: productData,
      page: page,
      pageCount: res._meta.pageCount,
    })
    if (page == this.data.pageCount) {
      this.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }
  },

  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = collection.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },
  
  //删除收藏
  toMove:function(e){
    var that = this;
    var id = collection.getDataSet(e,'id');
    collection.getCollectionDel(id,(data)=>{
      if(data.status==1){
        that._loadData();
      }
    })
  },
})